<?php echo $html->css('upload.css'); ?>
<div id="content">
<?php
if (!empty($has_category)) {
?>
<script type="text/javascript">
var category_id = <?php echo $category_id; ?>;
if (category_id == 0) {
    $(document).ready(function () {
        get_category_customfields($('#post_category').val());
    });
}

$(window).unload(function() {
        $.get('<?php echo $html->admin_url('/post_new/ajax_del_session/' . $upload_session); ?>');
});

function FCKeditor_OnComplete( editorInstance ) {
    editorInstance.Events.AttachEvent('OnSelectionChange', undisabled_save) ;
}

function undisabled_save() {
    $('#save').val('<?php __e('Save and Continue Editing'); ?>');
    document.getElementById("save").disabled = '';
}

function save_post(){
    $.post('<?php echo $html->admin_url('/post_new/ajax_save/' . $upload_session); ?>',{
        post_category:$('#post_category').val(),
        post_title:$('#post_title').val(),
        //post_content:tinyMCE.get('post_content').getContent(),
        post_content:FCKeditorAPI.GetInstance('data[Posts][post_content]').GetXHTML(),
        post_time:$('#post_time').val(),
        post_status:<?php echo ($post_type == 1) ? '$("input[@type=radio][@checked]").val()' : 1; ?>, 
        // post_status: 1 mean have been post, 2 mean just drafts.
        // post_type: 1 mean post, 2 mean page
        post_type:<?php echo $post_type; ?>,
        post_slug:$('#post_slug').val(),
        private_status:$('#private_status').val(),
        read_password_status:$('#read_password_status').val(),
        read_password:$('#read_password').val(),
        comment_status:$('#comment_status').val(),
        ping_status:$('#ping_status').val()
        
    },function(data) {
        $('#save').val('<?php __e('Saved'); ?>');
        document.getElementById("save").disabled="disabled";
        $('#drafts_count').html(data);
    });
}

function get_category_customfields(category_id) {
    $.get('<?php echo $html->admin_url('/post_new/ajax_get_customfields/'); ?>'+category_id+'/'+'<?php echo $upload_session; ?>', function (data) {
        $('.finalrow_customfields').remove();
        if (data != '') {
            $('#post_slug_tr').after(data);
        }
    });
}

function delete_thumbnail() {
    $.get('<?php echo $html->admin_url('/post_new/ajax_delete_thumbnail/'); ?>'+'<?php echo $upload_session; ?>', function (data) {
        if (data != '') {
            if (data == 'done') {
                $('#thumbnail_view').hide();
                $('#thumbnail_upload').show();
            }
        }
    });
}

function check_edit_form() {
    var post_slug = $('#post_slug').val();
    if (post_slug != '') {
        var int_post_slug = parseInt(post_slug).toString();
        if (post_slug == int_post_slug) {
            alert('<?php __e('Post slug can not be all numeric'); ?>');
            $('#post_slug').focus();
            return false;
        }
        
    }
}
</script>
<form id="post_form" name="post_form" method="post" action="<?php echo $html->admin_url('/post_new/post_new' . '?' . $this->referer); ?>" enctype="multipart/form-data" onsubmit="return check_edit_form();">
<?php echo $html->input('Posts/post_type', array('type'=>'hidden', 'value'=>$post_type)); ?>
<?php echo $html->input('Posts/upload_session', array('type'=>'hidden', 'value'=>$upload_session)); ?>
<table width="100%">
  <tr>
    <td>
<div id="post_meta">
    <table>
      <tr class="finalrow">
        <th><?php echo isset($diagram_parent_title) ? $diagram_parent_title : __('Post category'); ?></th>
        <td>
        <select name="data[Post2category][category_id]" id="post_category" <?php echo ($post_type == 1) ? 'onchange="get_category_customfields(this.value);"' : ''; ?> style="width:100%;">
        <?php echo $select_list; ?>
       </select>
        </td>
      </tr>

      <tr class="finalrow">
        <th><?php echo isset($page_title) ? $page_title : __('Title'); ?></th>
        <td><span class="errorbox-good">
            <?php echo $html->input('Posts/post_title', array('style'=>'width:100%', 'id'=>'post_title')); ?>
          </span> </td>
      </tr>
    </table>
</div>
<?php require ARLICLE . 'app/view/elements/fckeditor.php'; ?>
<div id="post_meta">
<table>
      <tr class="finalrow">
        <th></th>
        <td align="right"><!--<span <?php echo $post_status ? '' : 'style="display: none;"'; ?>>
<input name="save" id="save" type="button" value="<?=__('Save and Continue Editing')?>" onclick="save_post(); return false;" > </span> -->
<input name="publish" id="publish" style="font-weight: bold;" tabindex="5" accesskey="p" value="<?php echo isset($action_title) ? $action_title : __('Publish'); ?>" type="submit">&nbsp;&nbsp;
           </td>
      </tr>
</table>


<table>

    <tr class="finalrow">
    <th><?php __e('Thumbnail'); ?></th>
    <td>

<div id="thumbnail_view" style="<?php echo $thumbnail_view_style; ?>">
<img src="<?php echo $thumbnail_url; ?>" width="100px" height="100px"> &nbsp; &nbsp; <?php echo $html->admin_link(__('Delete and upload it again'), '#', array('onclick'=>"delete_thumbnail();return false;")); ?>
</div>
<div id="thumbnail_upload" style="<?php echo $thumbnail_upload_style; ?>">
    <input name="thumbnail" id="thumbnail" size="60" type="file">
</div>
</td>
    </tr>

      <tr class="finalrow" <?php echo $post_status ? '' : 'style="display: none;"'; ?>>
        <th><?=__('Status')?></th>
        <td>
<?php echo $html->radio('Posts/post_status',array('id'=>'post_status_public','value'=>1,'onclick'=>'hidden_readbypassword();')); ?><label for="post_status_public"> <?php __e('Public'); ?></label>
<?php echo $html->radio('Posts/post_status',array('id'=>'post_status_draft','value'=>2,'onclick'=>'hidden_readbypassword();')); ?><label for="post_status_draft"> <?php echo __e('Draft'); ?></label> 
    </td>
      </tr>
      <tr class="finalrow" style="display:none;">
        <th><?=__('Permission')?></th>
        <td>
    <?php echo $html->checkbox('Permissions/private_status', array('value'=>'1', 'id'=>'private_status')); ?>
    <label for="private_status"> <?php __e('Private'); ?></label>
    <?php echo $html->checkbox('Permissions/read_password', array('value'=>'1', 'id'=>'read_password_status', 'onclick'=>'set_readbypassword(this.checked);')); ?>
    <label for="read_password_status"><span class="text"> <?php __e('Read password'); ?></span></label>
    <span id="readbypassword" <?php echo $read_password_status; ?>> <?php echo $html->input('Posts/read_password', array('id'=>'read_password')); ?></span>
    <?php echo $html->checkbox('Permissions/comment_status', array('value'=>'1', 'id'=>'comment_status')); ?>
        <label for="comment_status"><span class="text"> <?php __e('Allow comments'); ?></span></label> 
        <?php echo $html->checkbox('Permissions/ping_status', array('value'=>'1', 'id'=>'ping_status')); ?>
        <label for="ping_status"><span class="text"> <?php __e('Allow pings'); ?></span></label>

        </td>
      </tr>
      <tr class="finalrow">
        <th><?=__('Post date')?></th>
        <td><?php echo $html->input('Posts/post_time', array('id'=>'post_time')); ?> <span class="u"> <?php __e('You can reserve it'); ?> <?php __e('ex'); ?>: <?php echo $reserve_time; ?></span></td>
      </tr>
      <tr class="finalrow" id="post_slug_tr">
        <th><?=__('Post slug')?></th>
        <td><?php echo $html->input('Posts/post_slug', array('id'=>'post_slug')); ?> <span class="u"> <?php __e('Slug can not be all numeric'); ?></span></td>
      </tr>
      <?php echo $custom_table; ?>
    </table>
</div>
    </td>
    <td width="250px" valign="top"></td>
  </tr>
</table>

</form>
<?php
} else {
    echo '<span class=\'new\'>',__('Please add the category first'),'</span>';
}
?>
</div>
